package com.gdufe.user.controller;

import com.gdufe.common.utils.R;
import com.gdufe.common.vo.AddPointsVo;
import com.gdufe.user.dto.PasswordDto;
import com.gdufe.common.entity.User;
import com.gdufe.user.service.UserClientService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/client")
@RequiredArgsConstructor
@Slf4j
public class UserClientController {

    @Autowired
    private UserClientService clientService;

    /**
     * 给用户增加积分
     */
    @RequestMapping("/addPoints")
    public R addPoints(@RequestBody AddPointsVo vo) {
        clientService.addPoints(vo);
        return R.ok();
    }

    /**
     * 获取用户信息
     */
    @RequestMapping("/init")
    public R info() {
        User user = clientService.getClientUser();

        return R.ok().put("info", user);
    }


    /**
     * 修改用户信息
     */
    @RequestMapping("/modify")
    public R update(@RequestBody User user) {
        clientService.updateById(user);

        return R.ok();
    }

    /**
     * 获取登录的用户信息
     *
     * @return
     */
    @GetMapping("/getUser")
    public R getClientUser() {
        log.info("获取登录的用户信息...");
        User user = clientService.getClientUser();
        return R.ok().setData(user);
    }

    /**
     * 根据id查询用户信息
     */
    @GetMapping("/{id}")
    public R userSelectById(@PathVariable Long id) {
        log.info("根据id查询用户，参数：{}", id);
        User user = clientService.getById(id);
        return R.ok().setData(user);
    }

    /**
     * 修改密码
     *
     * @return
     */
    @PutMapping("/updatePassword")
    public R updatePassword(@RequestBody PasswordDto passwordDto) {
        clientService.editPassword(passwordDto);
        return R.ok();
    }
}
